import {
    reqSpaceManager,
    reqProductModelPage,
    reqOrgList
} from "@api/api.js";
export default {
    data() {
        return {
            //实现时间限制选择在一个月内而且不能超过当前时间
            pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                    this.cuttentTime = minDate.getTime();
                    if (maxDate) {
                        this.cuttentTime = "";
                    }
                },
                disabledDate: (time) => {
                    //获取当前时间戳
                    let myDate = new Date();
                    let newY = myDate.getFullYear();
                    let newM = myDate.getMonth() + 1;
                    let newD = myDate.getDate();
                    let oldTime =
                        newY +
                        "-" +
                        (newM < 10 ? "0" + newM : newM) +
                        "-" +
                        (newD < 10 ? "0" + newD : newD) +
                        " " +
                        "23:59:59";
                    // let oldTime="2022-7-5 23:59:59"
                    oldTime = new Date(oldTime).getTime(); // 将中国时间转换成毫秒
                    // 今天之后的时间不可选
                    if (time.getTime() > oldTime) {
                        //time.getTime() > Date.now()
                        return true;
                    }
                    //选择日期范围为一个月
                    // if (this.cuttentTime != "") {
                    //     const one = 30 * 24 * 3600 * 1000;
                    //     const minTime = this.cuttentTime - one;
                    //     const maxTime = this.cuttentTime + one;
                    //     return time.getTime() < minTime || time.getTime() > maxTime;
                    // }
                },
            },
            timeArr: [],
            orgName: '',
            selectedValue: "",

            sysInstanceIndex2: 1,
            sysInstanceTotalPages2: 10,
            loading2: false,
            buildings: [],

            buildingsName2: "",
            buildingsName: "",
            classMarkList: [],
            hasSearch: false,
        };
    },
    mounted() {
        if (this.user.roleName == 'INSTERALL_USER' || this.user.roleName == 'UNIT_ADMIN') {
            this.getOrgList()
        }
    },
    methods: {
        // 搜索
        async searchClick1() {
            this.page = 1;
            this.limit = 10;
            this.showPopover = false;
            await this.getDataList();
            this.clearSelections();
        },
        // 高级搜索重置
        async resetClick() {
            // this.handleClear()
            this.selectedValue = '';
            this.orgName = '';
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.parentId = ''

            this.searchForm.online = ""
            this.searchForm.productId = ""
            this.searchForm.orgId = ""
            this.searchForm.minBattery = ""
            this.searchForm.maxBattery = ""
            this.searchForm.isAlarm = ""
            this.searchForm.validity = ''
            this.searchForm.classMark = ''
            this.searchForm.startTime = ''
            this.searchForm.endTime = ''
            this.searchForm.remark = ''
            this.timeArr = []
            this.classMarkList = []
            this.buildings = []
            this.targetId = this.spacesManage.organizationInfo.orgId;
            if (this.$refs.refTreeSearch) {
                this.$refs.refTreeSearch.onclose()
            }

            this.page = 1;
            this.limit = 10;
            this.searchForm.buildingId = ""; // 清空建筑物

            this.page = 1;
            this.limit = 10;
            await this.getDataList();
            this.clearSelections();

            if (this.user.roleName != 'INSTERALL_USER' && this.user.roleName != 'UNIT_ADMIN') {
                this.orgList = []
            }
        },
        changeunitId(e) {
            this.page = 1;
            this.limit = 10;
            this.searchForm.buildingId = ""; // 清空建筑物
            this.buildings = []
            if (e != '') {
                this.clearBuildings(); //建筑物
            }
        },
        // 清空
        handleClear() {
            this.selectedValue = '';
            this.orgName = '';
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.orgList = []
            this.searchForm.buildingId = ""; // 清空建筑物
            this.buildings = []
            this.getOrgList()
        },
        // 远程搜索
        async remoteMethod(query) {
            this.selectedValue = query //保留输入的值
            this.orgName = query;
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.orgList = []
            this.getOrgList()
        },
        async loadMore() {
            if (this.sysInstanceIndex >= Math.ceil(this.sysInstanceTotalPages / 10)) return
            this.sysInstanceIndex++
                this.loading = true;
            // 获取列表数据的方法
            await this.getOrgList()
            this.loading = false;
        },
        // 请求单位列表
        async getOrgList() {
            let { tenantId, userId, roleName } = this.user;
            const res = await reqOrgList({
                tenantId,
                userId,
                roleName,
                orgName: this.orgName,
                parentId: this.parentId,
                type: "4"
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.orgList = res.data.rows
        },
        // 选择机构
        changeOrgId(instanceId) {
            // this.page = 1;
            // this.limit = 10;
            // this.targetId = instanceId.id;
            // this.searchForm.buildingId = ""; // 清空建筑物
            // this.clearBuildings(); //建筑物
            this.searchForm.buildingId = ""; // 清空建筑物
            this.buildings = []
            this.parentId = instanceId.id
            this.handleClear();
        },

        // 清空建筑物
        clearBuildings() {
            this.buildingsName = '';
            this.buildingsName2 = '';
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.buildings = []
                // if (this.searchForm.orgId != '') {
            this.getBuildings()
                // }
        },
        // 远程搜索
        async remoteBuildings(query) {
            this.buildingsName = query; //保留输入的值
            this.buildingsName2 = query; //保留输入的值
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.buildings = []
            this.getBuildings()
        },
        // 建筑物操作
        async loadBuildings() {
            if (this.sysInstanceIndex2 >= Math.ceil(this.sysInstanceTotalPages2 / 10)) return
            this.sysInstanceIndex2++;
            this.loading2 = true;
            // 获取列表数据的方法
            await this.getBuildings()
            this.loading2 = false;
        },
        // 建筑物列表
        async getBuildings() {
            let res = await reqSpaceManager(this.sysInstanceIndex2, 10, {
                userId: this.user.userId,
                roleName: this.user.roleName,
                metaCode: 1,
                orgId: this.selectedValue,
                isStatistics: 0,
                name: this.buildingsName2
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages2 = res.data.total
            this.buildings = [...this.buildings, ...res.data.rows]
        },
        // 选择建筑物
        changeBuildings(e) {
            this.searchForm.buildingId = e
        },
        // 获取产品型号
        async ProductModelPage(e) {
            this.classMarkList = [];
            this.searchForm.classMark = "";
            if (!e) return;
            let res = await reqProductModelPage(1, 30, {
                productId: this.searchForm.productId,
            });
            // console.log(res);
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.classMarkList = res.data.rows
        },
    },
};